package edu.guet.server.service;

import edu.guet.server.mapper.SemesterMapper;
import edu.guet.server.mapper.StuSemesterMapper;
import edu.guet.server.mapper.UserMapper;
import edu.guet.server.model.SemesterStatistic.SemesterPermissions;
import edu.guet.server.model.Stu;
import edu.guet.server.model.dto.StuSemDTO;
import edu.guet.server.model.dto.StudentScoreDTO;
import edu.guet.server.utils.ResultAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StuSemService {
    private static final Logger log = LoggerFactory.getLogger(StuSemService.class);
    @Autowired
    SemesterMapper semesterMapper;
    @Autowired
    private StuSemesterMapper stuSemesterMapper;
    @Qualifier("userMapper")
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StuSemesterService stuSemesterService;

    /**
     * 根据用户id找到关联的学期课程ids
     * @param userId
     * @return
     */
    public List<Integer> findsemesterIdsByuserId(Integer userId) {
        return semesterMapper.findsemesterIdsByuserId(userId);
    }

    /**
     * 查看是否加课
     * @param stuSemDTO
     * @return
     */
    public boolean findStuSem(StuSemDTO stuSemDTO) {
        StuSemDTO stuSem = semesterMapper.findStuSem(stuSemDTO);
        if (stuSem != null) {
            return true;
        }else return false;
    }

    /**
     * 加课
     *
     * @param stus
     * @param semester_id
     */
    @Transactional
    public ResultAPI addStuSem(List<Stu> stus, Integer semester_id) {
        List<Stu> stuList=userMapper.getStulistBynumber(stus);
        boolean flag=semesterMapper.insertSemStus(stuList,semester_id);
        log.info("批量添加："+ flag);
        if (flag) {
            return ResultAPI.success();
        }else {
            return ResultAPI.error("添加失败！");
        }
    }

    public List<StudentScoreDTO> findStudentScores(Integer semesterId) {
        return semesterMapper.findStudentScores(semesterId);
    }

    public List<Stu> getStuList(Integer semesterId) {
        return semesterMapper.getStuList(semesterId);
    }

    @Transactional
    public ResultAPI editStuScore(StudentScoreDTO editData) {
        //获取权重
        SemesterPermissions semesterPermissions=semesterMapper.findPermissionsById(editData.getSemester_id());
        //更新该学生的平均成绩，并同时计算有效成绩
        Double validScore=editData.getHomeworkScore()*semesterPermissions.getHmkPermission()+editData.getTestScore()*semesterPermissions.getTestPermission();
        validScore=Math.round(validScore*100)/100.0;
        editData.setTotalScore(validScore);
        boolean flag=stuSemesterMapper.updateStuSemSocre(editData);

        if(flag){
            return ResultAPI.success(editData);
        }else{
            return ResultAPI.error("无更新");
        }
    }
}
